const request=require('request');
const fs=require('fs');
const {spawn}=require('child_process');


let html=fs.readFileSync('./bilibili_html.txt',{
    encoding:'utf-8'
});

let title=html.match(/<title.+<\/title>/gi)[0].split('_')[0].replace('<title data-vue-meta="true">','');


let startPos=html.indexOf('window.__playinfo__=')
let endPos=html.indexOf('</script>',startPos)

let videoInfoTxt=html.substring(startPos+20,endPos)
console.log(videoInfoTxt)
let videoInfo=JSON.parse(videoInfoTxt)

let videoUrl=''
let audioUrl=''

if (videoInfo.data.dash){
    let videos=videoInfo.data.dash.video
    videoUrl=videos[videos.length-1].baseUrl
    audioUrl=videoInfo.data.dash.audio[0].baseUrl
    
    console.log('视频地址')
    console.log(videoUrl)
    console.log('音频地址')
    console.log(audioUrl)

    planA()
}else{
    videoUrl=videoInfo.data.durl[0].url

    console.log('视频地址')
    console.log(videoUrl)

    planB()
}


function planA(){
    let ws=fs.createWriteStream('./bilibili_v',{
        flags:'a+',
        encoding:null
    })
    
    console.log('开始下载视频')
    request({
        url:videoUrl,
        headers:{
            Referer: 'https://www.bilibili.com/',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        }
    }).pipe(ws).on('close',()=>{
        // 第一个回调
        console.log('视频下载完成')
    
        let ws2=fs.createWriteStream('./bilibili_a',{
            flags:'a+',
            encoding:null
        })
        
        
        request({
            url:audioUrl,
            headers:{
                Referer: 'https://www.bilibili.com/',
                'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
            }
        }).on('error',(e)=>{
            console.log('视频下载出错')
            console.log(e)
        }).pipe(ws2).on('close',()=>{
            // 第二个回调
            console.log('音频下载完成')
    
    
            console.log('ffmpeg合并');
            let margeTT=spawn('ffmpeg.exe',['-i','bilibili_v','-i','bilibili_a','-c','copy',`${title}.mp4`]);
        
            margeTT.stdout.on('data', function (data) {
                console.log('stdout: \r\n' + data);
            });
            
            margeTT.stderr.on('data', function (data) {
                console.log('stderr: \r\n' + data);
            });
        
            margeTT.on('exit', function (code) {
    
                // 第三个回调
                console.log('程序退出' + code);
                
                fs.unlinkSync('./bilibili_v');
                fs.unlinkSync('./bilibili_a');
    
                console.log('已删除所有临时文件,结束')
            });
            
        })
    
    })
    
}

function planB(){
    let ws=fs.createWriteStream(`./${title}.flv`,{
        flags:'a+',
        encoding:null
    })
    
    console.log('开始下载视频')
    request({
        url:videoUrl,
        headers:{
            Referer: 'https://www.bilibili.com/',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        }
    }).pipe(ws).on('close',()=>{
        // 第一个回调
        console.log('视频下载完成')
    })
}

